package com.mkvld.appietijdenv2.dialogs;
import java.security.InvalidParameterException;
import java.text.SimpleDateFormat;

import com.mkvld.appietijdenv2.R;
import com.mkvld.appietijdenv2.activities.ActMainV9;
import com.mkvld.appietijdenv2.cloud.SyncServiceV9;
import com.mkvld.appietijdenv2.data.Shift;
import com.mkvld.appietijdenv2.data.Utils;
import com.mkvld.appietijdenv2.databases.ShiftDatabase;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
/**
 * NewShiftDialog is a FragmentDialog which allows the user to add a shift
 * @author Remco
 *
 */
public class DialNewShiftV9 extends DialogFragment {
	protected Shift shift, prevShift;
	private boolean prev;
	Spinner startHour, startMin, endHour, endMin, type;
	String[] types;
	String day;
	protected AlertDialog dialog;
	
	
	
	public static void showNew(DialNewShiftV9 dialog, FragmentActivity activity, int year, int week, int day) {
		Bundle args = new Bundle();
		args.putSerializable("shift", new Shift(year, week, day));
		dialog.setArguments(args);
		dialog.show(activity.getSupportFragmentManager(), "nsd");
	}
	
	public static void showEdit(DialNewShiftV9 dialog, FragmentActivity activity, Shift shift) {
		Bundle args = new Bundle();
		args.putSerializable("shift", shift);
		args.putBoolean("isPrev", true);
		dialog.setArguments(args);
		dialog.show(activity.getSupportFragmentManager(), "nsd");
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		shift = (Shift)getArguments().get("shift");
		prev = getArguments().getBoolean("isPrev");
		if (shift == null)
			throw new InvalidParameterException();
		if (prev)
			prevShift = shift.clone();
		SimpleDateFormat sdf = new SimpleDateFormat("EE d-M-y", Utils.locale);
		day = sdf.format(shift.getDate());
	}
	
	protected double getStart() {
		return getStartHour() + (double)getStartMin() / 4;
	}
	protected double getEnd() {
		return getEndHour() + (double)getEndMin() / 4;
	}
	
	protected int getLayoudId() {
		return R.layout.dial_newshift;
	}
	
	protected int getStartHour() {
		return startHour.getSelectedItemPosition();
	}
	protected int getStartMin() {
		return startMin.getSelectedItemPosition();
	}
	protected int getEndHour() {
		return endHour.getSelectedItemPosition();
	}
	protected int getEndMin() {
		return endMin.getSelectedItemPosition();
	}
	protected void setStartHour(int startHour) {
		this.startHour.setSelection(startHour);
	}
	protected void setStartMin(int startMin) {
		this.startMin.setSelection(startMin);
	}
	protected void setEndHour(int endHour) {
		this.endHour.setSelection(endHour);
	}
	protected void setEndMin(int endMin) {
		this.endMin.setSelection(endMin);
	}
	
	AdapterView.OnItemSelectedListener changeListener =
			new AdapterView.OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(getEnd() > getStart());
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {}
		
	};
	
	protected void initView(View root) {
		((TextView)root.findViewById(R.id.dns_day)).setText(day);
		
		startHour = (Spinner) root.findViewById(R.id.dns_val_from_hour);
		startHour.setOnItemSelectedListener(changeListener);
		
		startMin = (Spinner) root.findViewById(R.id.dns_val_from_min);
		startMin.setOnItemSelectedListener(changeListener);
		
		
		endHour = (Spinner) root.findViewById(R.id.dns_val_to_hour);
		endHour.setOnItemSelectedListener(changeListener);
		
		endMin = (Spinner) root.findViewById(R.id.dns_val_to_min);
		endMin.setOnItemSelectedListener(changeListener);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		View v = getActivity().getLayoutInflater().inflate(getLayoudId(), null, false);
		initView(v);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(R.string.dns_title);
		builder.setView(v);
		
		builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				shift.setStart(getStart());
				shift.setEnd(getEnd());
				if (!prev) {
					ShiftDatabase.db.addShift(shift);
					SyncServiceV9.addCloudShift(getActivity(), shift);
					//getActivity().startService(SyncServiceV9.createAddIntent(getActivity(), shift));
				} else {
					ShiftDatabase.db.editShift(prevShift, shift);
					SyncServiceV9.editCloudShift(getActivity(), shift, prevShift);
					//getActivity().startService(SyncServiceV9.createEditIntent(getActivity(), shift, prevShift));
				}
				((ActMainV9)getActivity()).update();
			}
		});
		dialog = builder.create();
		return dialog;
	}
}

